<template>
  <Drawer
      v-model="visible"
      title="地图源数据编辑"
      width="450"
      :mask-closable="false"
      :styles="$PnUtil.buildDrawerStyleByHasFooter()">

    <Form ref="form" :label-width="100" :model="formData" :rules="ruleValidate">
      <FormItem label="id" prop="id" hidden>
        <Input v-model="formData.id"/>
      </FormItem>
      <FormItem label="AdCode" prop="adcode">
        <Input v-model="formData.adcode" placeholder="请输AdCode" :disabled="formData.id != ''"/>
      </FormItem>
      <FormItem label="地图名称" prop="alias_name">
        <Input v-model="formData.alias_name" placeholder="请输地图名称"/>
      </FormItem>
      <FormItem label="等级" prop="level">
        <Select v-model="formData.level">
          <Option value="province">省份</Option>
          <Option value="city">城市</Option>
          <Option value="country">国家</Option>
        </Select>
      </FormItem>
      <FormItem label="备注" prop="remark">
        <Input type="textarea" :rows="4" v-model="formData.remark"/>
      </FormItem>
      <FormItem label="地图JSON数据" prop="json_data">
        <CodeEditor v-model="formData.json_data" mode="json"></CodeEditor>
      </FormItem>
    </Form>

    <DrawerFooter>
      <Button class="m-r-5px" @click="visible = false">关闭</Button>
      <Button type="primary" @click="submitForm" :loading="loading">保存</Button>
    </DrawerFooter>
  </Drawer>
</template>

<script>
import ManageMixin from "@/mixin/ManageMixin";

export default {
  name: "MapGeoJsonForm",
  mixins: [ManageMixin],
  data() {
    return {
      visible: false,
      loading: false,
      formData: {
        id: '',
        adcode: '',
        alias_name: '',
        level: '',
        remark: '',
        json_data: ''
      },
      ruleValidate: {
        adcode: [
          {required: true, message: 'AdCode不能为空', trigger: 'blur'}
        ],
        alias_name: [
          {required: true, message: '地图名称不能为空', trigger: 'blur'}
        ],
        level: [
          {required: true, message: '等级不能为空', trigger: 'blur'}
        ],
      },
    }
  },
  mounted() {

  },
  methods: {
    init(type, id) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (type == 'edit') {
          this.$PnApi.MapGeoJsonApi.getMapGeoJsonById(id).then(result => {
            this.formData = this.recover(this.formData, result.data.payload)
          })
        }
      })
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$PnApi.MapGeoJsonApi.saveMapGeoJson(this.formData).then(result => {
            if (result.data.success) {
              this.visible = false
              this.$Message.success(result.data.message)
              this.$emit("refreshDataList")
            }else {
              this.$Message.error(result.data.message)
            }
            this.loading = false
          })
        }
      });
    }
  },
  computed: {}
}
</script>

<style scoped>
>>> .ivu-form-item-content {
  line-height: initial !important;
}
</style>
